﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MiniChat
{
   public class Ullity
    {
        public Ullity ()
		{

		}
        public static String EncodeString(String str)
        {
            str = str.Replace(":", "xhaichamx");
            str = str.Replace(",", "xphayx");
            str = str.Replace("|", "xseperatorx");
            str = str.Replace(";", "xchamphayx");
            str = str.Replace("\n", "xxuongdongx");
            return str;

        }
        public static String DecodeString(String str)
        {
            str = str.Replace( "xhaichamx",":");
            str = str.Replace( "xphayx",",");
            str = str.Replace("xseperatorx","|");
            str = str.Replace("xchamphayx",";");
            str = str.Replace("xxuongdongx","\n");
            return str;
        }
        public static String MD5Gen(string str)
		{
			using(MD5 Md5hash = MD5.Create())
			{
				byte []data = Md5hash.ComputeHash(Encoding.UTF8.GetBytes(str));
				StringBuilder sBuilder = new StringBuilder();
				for (int i = 0; i < data.Length; i++)
				{
					sBuilder.Append(data[i].ToString("x2"));
				}
				return sBuilder.ToString();
			}
		}
		public static string Trim(String s)
		{
			if(s == null){return null;}
			return s.Trim();
		}
		public static String Trim(Object s)
		{
			if(s == null){return null;}
			return s.ToString().Trim();
		}
		public static void DebugString(String str)
		{


			foreach(char c in str)
			{
				Console.Write (((int)c).ToString());
			}
			Console.Write("\n");

		}
		public static void Log(String str){

			Console.WriteLine("\"" + str + "\"");

		}

		/// <summary>
		///  Log 
		/// </summary>
		/// <param name="str">String.</param>
		/// <param name="err">
		///  Bằng 1 -> Lỗi hệ thống 
		///  Bằng 2 -> Cảnh báo
		///  Bằng 3 -> Thông tin
		///  Bằng 4 hoặc khác -> Ghi chú
		/// .</param>
		public static void Log(String str, int err)
		{
			switch(err)
			{
			case 1:
				Console.WriteLine ("Err : {0}",str);break;
			case 2:
				Console.WriteLine ("Notice : {0}",str);break;
			case 3:
				Console.WriteLine ("Info : {0}",str);break;
			default:
				Console.WriteLine (str);break;
			}
		}

		public static String []Split(String str,string sp = null)
		{
			if(str == null){return null;}
			if(sp == null){return new string[]{str};}
			return str.Split(new string[]{sp},StringSplitOptions.RemoveEmptyEntries);
		}
		public static bool Instr(string stror, string strcheck){
			if(stror == null)return false;
			return stror.Contains(strcheck);
		}

		public static String MyPath(){
			return Directory.GetCurrentDirectory();
		}
		public static string GenHash()
		{
			return Guid.NewGuid().ToString("N");
		}
    }
}
